﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminBackup : BaseAdminModule
    {
        private string _BType;
        [AccessedThroughProperty("cmdDoBackup")]
        private LinkButton _cmdDoBackup;
        [AccessedThroughProperty("cmdDoImport")]
        private LinkButton _cmdDoImport;
        [AccessedThroughProperty("chkArchiveProd")]
        private CheckBox _chkArchiveProd;
        [AccessedThroughProperty("chkCasCade")]
        private CheckBox _chkCasCade;
        [AccessedThroughProperty("chkCreateCat")]
        private CheckBox _chkCreateCat;
        [AccessedThroughProperty("chkExpOrders")]
        private CheckBox _chkExpOrders;
        [AccessedThroughProperty("ddlSelectCategory")]
        private DropDownList _ddlSelectCategory;
        [AccessedThroughProperty("FileUploadDocs")]
        private FileUpload _FileUploadDocs;
        [AccessedThroughProperty("FileUploadModel")]
        private FileUpload _FileUploadModel;
        [AccessedThroughProperty("FileUploadShip")]
        private FileUpload _FileUploadShip;
        [AccessedThroughProperty("FileUploadXML")]
        private FileUpload _FileUploadXML;
        [AccessedThroughProperty("FileUploadZip")]
        private FileUpload _FileUploadZip;
        [AccessedThroughProperty("lblMsg")]
        private Label _lblMsg;
        [AccessedThroughProperty("plAllOrders")]
        private UserControl _plAllOrders;
        [AccessedThroughProperty("plAllProducts")]
        private UserControl _plAllProducts;
        [AccessedThroughProperty("plArchiveProd")]
        private UserControl _plArchiveProd;
        [AccessedThroughProperty("plCreateCat")]
        private UserControl _plCreateCat;
        [AccessedThroughProperty("plImportModel")]
        private UserControl _plImportModel;
        [AccessedThroughProperty("plImportNew")]
        private UserControl _plImportNew;
        [AccessedThroughProperty("plImportShipping")]
        private UserControl _plImportShipping;
        [AccessedThroughProperty("plImportUpdate")]
        private UserControl _plImportUpdate;
        [AccessedThroughProperty("plProductDocs")]
        private UserControl _plProductDocs;
        [AccessedThroughProperty("plProductImages")]
        private UserControl _plProductImages;
        [AccessedThroughProperty("plPurgeStore")]
        private UserControl _plPurgeStore;
        [AccessedThroughProperty("plShipping")]
        private UserControl _plShipping;
        [AccessedThroughProperty("plXMLFile")]
        private UserControl _plXMLFile;
        [AccessedThroughProperty("plZipDocs")]
        private UserControl _plZipDocs;
        [AccessedThroughProperty("plZipFile")]
        private UserControl _plZipFile;
        [AccessedThroughProperty("pnlBackup")]
        private Panel _pnlBackup;
        [AccessedThroughProperty("pnlRestore")]
        private Panel _pnlRestore;
        [AccessedThroughProperty("rbAllOrders")]
        private RadioButton _rbAllOrders;
        [AccessedThroughProperty("rbAllProducts")]
        private RadioButton _rbAllProducts;
        [AccessedThroughProperty("rbImportNew")]
        private RadioButton _rbImportNew;
        [AccessedThroughProperty("rbImportUpdate")]
        private RadioButton _rbImportUpdate;
        [AccessedThroughProperty("rbProductDocs")]
        private RadioButton _rbProductDocs;
        [AccessedThroughProperty("rbProductImages")]
        private RadioButton _rbProductImages;
        [AccessedThroughProperty("rbPurgeStore")]
        private RadioButton _rbPurgeStore;
        [AccessedThroughProperty("rbShipping")]
        private RadioButton _rbShipping;

        public AdminBackup()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        private void cmdDoBackup_Click(object sender, EventArgs e)
        {
            Export export = new Export();
            string strBackupFile = "";
            SharedFunctions.CreateDir(this.PortalSettings, "productdocs");
            SharedFunctions.CreateDir(this.PortalSettings, "productimages");
            string categoryList = "";
            if (Conversions.ToDouble(this.ddlSelectCategory.SelectedValue.ToString()) >= 0.0)
            {
                if (this.chkCasCade.Checked)
                {
                    CategoryController controller = new CategoryController();
                    ArrayList categories = controller.GetCategories(this.PortalId, SharedFunctions.GetCurrentCulture());
                    categoryList = controller.GetSubCategoryList(categories, Conversions.ToInteger(this.ddlSelectCategory.SelectedValue));
                }
                categoryList = categoryList + this.ddlSelectCategory.SelectedValue.ToString();
            }
            if (this.rbAllOrders.Checked)
            {
                strBackupFile = this.PortalSettings.HomeDirectoryMapPath + this.PortalAlias.HTTPAlias + "_ExportOrders.xml";
                strBackupFile = export.ExportOrders(this.PortalId, false, strBackupFile);
            }
            if (this.rbAllProducts.Checked)
            {
                strBackupFile = this.PortalSettings.HomeDirectoryMapPath + this.PortalAlias.HTTPAlias + "_ExportProducts.xml";
                strBackupFile = export.ExportProducts(this.PortalId, strBackupFile, this.chkExpOrders.Checked, categoryList);
            }
            if (this.rbProductImages.Checked)
            {
                strBackupFile = this.PortalSettings.HomeDirectoryMapPath + this.PortalAlias.HTTPAlias + "_ExportImages.zip";
                strBackupFile = export.ExportImages(this.PortalId, strBackupFile, categoryList);
            }
            if (this.rbProductDocs.Checked)
            {
                strBackupFile = this.PortalSettings.HomeDirectoryMapPath + this.PortalAlias.HTTPAlias + "_ExportDocs.zip";
                strBackupFile = export.ExportDocs(this.PortalId, strBackupFile);
            }
            if (this.rbShipping.Checked)
            {
                strBackupFile = this.PortalSettings.HomeDirectoryMapPath + this.PortalAlias.HTTPAlias + "_ExportShipping.xml";
                strBackupFile = export.ExportShipping(this.PortalId, strBackupFile);
            }
            if (this.rbPurgeStore.Checked)
            {
                strBackupFile = this.PortalSettings.HomeDirectoryMapPath + this.PortalAlias.HTTPAlias + "_ExportPurged.xml";
                strBackupFile = export.ExportOrders(this.PortalId, true, strBackupFile);
                if (strBackupFile != "")
                {
                    OrderController controller2 = new OrderController();
                    ProductController controller3 = new ProductController();
                    controller2.PurgeArchivedOrders(this.PortalId);
                    controller3.PurgeProducts(this.PortalId);
                    controller3.ImageValidation(this.PortalId, this.MapPath(@"\"), true);
                    controller3.PurgeImages(this.PortalSettings, "productimages");
                    controller3.DocValidation(this.PortalId, this.PortalSettings.HomeDirectoryMapPath + "productdocs", true);
                    controller3.PurgeDocs(this.PortalSettings, "productdocs");
                    controller3.PurgeModels(this.PortalId);
                }
            }
            if (strBackupFile != "")
            {
                this.ForceDownload(strBackupFile);
            }
            else
            {
                this.lblMsg.Text = DotNetNuke.Services.Localization.Localization.GetString("ExportError", this.LocalResourceFile);
            }
        }

        private void cmdDoImport_Click(object sender, EventArgs e)
        {
            Import import = new Import();
            ImportCSV tcsv = new ImportCSV();
            string str7 = "";
            string filename = this.PortalSettings.HomeDirectoryMapPath + "ImportProducts.csv";
            string str6 = this.PortalSettings.HomeDirectoryMapPath + "ImportModels.csv";
            string str2 = this.PortalSettings.HomeDirectoryMapPath + "ImportProducts.xml";
            string str5 = this.PortalSettings.HomeDirectoryMapPath + "ImportProductImages.zip";
            string str3 = this.PortalSettings.HomeDirectoryMapPath + "ImportProductDocs.zip";
            string str4 = this.PortalSettings.HomeDirectoryMapPath + "ImportShipping.xml";
            if (this.FileUploadXML.FileName != "")
            {
                if (Path.GetExtension(this.FileUploadXML.FileName).ToLower() == ".csv")
                {
                    this.FileUploadXML.SaveAs(filename);
                    string s = SharedFunctions.GetStoreSetting(this.PortalId, "rowdelimeter.importmapping", "None");
                    string str8 = SharedFunctions.GetStoreSetting(this.PortalId, "fielddelimeter.importmapping", "None");
                    string str10 = SharedFunctions.GetStoreSetting(this.PortalId, "fieldqualifier.importmapping", "None");
                    string str9 = SharedFunctions.GetStoreSetting(this.PortalId, "fieldescapedqualifier.importmapping", "None");
                    s = HttpUtility.HtmlDecode(s);
                    str8 = HttpUtility.HtmlDecode(str8);
                    str10 = HttpUtility.HtmlDecode(str10);
                    str9 = HttpUtility.HtmlDecode(str9);
                    str7 = tcsv.ImportCSVProducts(this.PortalSettings, filename, this.rbImportUpdate.Checked, this.chkCreateCat.Checked, s, str8, str10, str9);
                }
                else if (Path.GetExtension(this.FileUploadXML.FileName).ToLower() == ".xml")
                {
                    this.FileUploadXML.SaveAs(str2);
                    if (this.chkArchiveProd.Checked)
                    {
                        IEnumerator enumerator=null;
                        ProductController controller = new ProductController();
                        ArrayList list = controller.GetProductList(this.PortalId, -1, SharedFunctions.GetCurrentCulture(), true);
                        try
                        {
                            enumerator = list.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                ProductListInfo current = (ProductListInfo) enumerator.Current;
                                NB_Store_ProductsInfo product = controller.GetProduct(current.ProductID, current.Lang);
                                product.Archived = true;
                                controller.UpdateObjProduct(product);
                            }
                        }
                        finally
                        {
                            if (enumerator is IDisposable)
                            {
                                (enumerator as IDisposable).Dispose();
                            }
                        }
                    }
                    str7 = import.ImportProducts(this.PortalSettings, str2, this.rbImportUpdate.Checked, this.chkCreateCat.Checked);
                }
                else
                {
                    str7 = DotNetNuke.Services.Localization.Localization.GetString("InvalidImportFile", this.LocalResourceFile);
                }
            }
            if (this.FileUploadZip.FileName != "")
            {
                this.FileUploadZip.SaveAs(str5);
                str7 = str7 + import.ImportProductImages(this.PortalSettings, str5);
            }
            if (this.FileUploadDocs.FileName != "")
            {
                this.FileUploadDocs.SaveAs(str3);
                str7 = str7 + import.ImportProductDocs(this.PortalSettings, str3);
            }
            if (this.FileUploadShip.FileName != "")
            {
                this.FileUploadShip.SaveAs(str4);
                str7 = str7 + import.ImportShipping(this.PortalSettings, str4);
                DataCache.RemoveCache("DefaultShipMethod" + this.PortalId.ToString());
            }
            if ((this.FileUploadModel.FileName != "") && (Path.GetExtension(this.FileUploadModel.FileName).ToLower() == ".csv"))
            {
                this.FileUploadModel.SaveAs(str6);
                string str16 = SharedFunctions.GetStoreSetting(this.PortalId, "rowdelimeter.importmapping", "None");
                string str13 = SharedFunctions.GetStoreSetting(this.PortalId, "fielddelimeter.importmapping", "None");
                string str15 = SharedFunctions.GetStoreSetting(this.PortalId, "fieldqualifier.importmapping", "None");
                string str14 = SharedFunctions.GetStoreSetting(this.PortalId, "fieldescapedqualifier.importmapping", "None");
                str16 = HttpUtility.HtmlDecode(str16);
                str13 = HttpUtility.HtmlDecode(str13);
                str15 = HttpUtility.HtmlDecode(str15);
                str14 = HttpUtility.HtmlDecode(str14);
                str7 = tcsv.ImportCSVModels(this.PortalSettings, str6, str16, str13, str15, str14);
            }
            this.lblMsg.Text = str7;
        }

        private void ForceDownload(string DownloadFile)
        {
            this.Response.AppendHeader("content-disposition", "attachment; filename=" + Path.GetFileName(DownloadFile));
            this.Response.ContentType = "application/octet-stream";
            this.Response.WriteFile(DownloadFile);
            this.Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.pnlBackup.Visible = false;
                this.pnlRestore.Visible = false;
                this.chkExpOrders.Text = DotNetNuke.Services.Localization.Localization.GetString("chkExpOrders", this.LocalResourceFile);
                this.chkCasCade.Text = DotNetNuke.Services.Localization.Localization.GetString("chkCasCade", this.LocalResourceFile);
                switch (this.Request.QueryString["spg"])
                {
                    case "bck":
                        this._BType = this.Request.QueryString["spg"];
                        break;

                    case "res":
                        this._BType = this.Request.QueryString["spg"];
                        break;

                    case null:
                        return;
                }
                if (!this.Page.IsPostBack)
                {
                    SharedFunctions.populateCategoryList(this.PortalId, this.ddlSelectCategory, "-1", DotNetNuke.Services.Localization.Localization.GetString("All", this.LocalResourceFile), "-1");
                    switch (this._BType)
                    {
                        case "bck":
                            this.pnlBackup.Visible = true;
                            this.pnlRestore.Visible = false;
                            break;

                        case "res":
                            this.pnlBackup.Visible = false;
                            this.pnlRestore.Visible = true;
                            break;
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        protected virtual LinkButton cmdDoBackup
        {
            get
            {
                return this._cmdDoBackup;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdDoBackup_Click);
                if (this._cmdDoBackup != null)
                {
                    this._cmdDoBackup.Click -= handler;
                }
                this._cmdDoBackup = value;
                if (this._cmdDoBackup != null)
                {
                    this._cmdDoBackup.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdDoImport
        {
            get
            {
                return this._cmdDoImport;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdDoImport_Click);
                if (this._cmdDoImport != null)
                {
                    this._cmdDoImport.Click -= handler;
                }
                this._cmdDoImport = value;
                if (this._cmdDoImport != null)
                {
                    this._cmdDoImport.Click += handler;
                }
            }
        }

        protected virtual CheckBox chkArchiveProd
        {
            get
            {
                return this._chkArchiveProd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkArchiveProd = value;
            }
        }

        protected virtual CheckBox chkCasCade
        {
            get
            {
                return this._chkCasCade;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkCasCade = value;
            }
        }

        protected virtual CheckBox chkCreateCat
        {
            get
            {
                return this._chkCreateCat;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkCreateCat = value;
            }
        }

        protected virtual CheckBox chkExpOrders
        {
            get
            {
                return this._chkExpOrders;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkExpOrders = value;
            }
        }

        protected virtual DropDownList ddlSelectCategory
        {
            get
            {
                return this._ddlSelectCategory;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ddlSelectCategory = value;
            }
        }

        protected virtual FileUpload FileUploadDocs
        {
            get
            {
                return this._FileUploadDocs;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._FileUploadDocs = value;
            }
        }

        protected virtual FileUpload FileUploadModel
        {
            get
            {
                return this._FileUploadModel;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._FileUploadModel = value;
            }
        }

        protected virtual FileUpload FileUploadShip
        {
            get
            {
                return this._FileUploadShip;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._FileUploadShip = value;
            }
        }

        protected virtual FileUpload FileUploadXML
        {
            get
            {
                return this._FileUploadXML;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._FileUploadXML = value;
            }
        }

        protected virtual FileUpload FileUploadZip
        {
            get
            {
                return this._FileUploadZip;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._FileUploadZip = value;
            }
        }

        protected virtual Label lblMsg
        {
            get
            {
                return this._lblMsg;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblMsg = value;
            }
        }

        protected virtual UserControl plAllOrders
        {
            get
            {
                return this._plAllOrders;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plAllOrders = value;
            }
        }

        protected virtual UserControl plAllProducts
        {
            get
            {
                return this._plAllProducts;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plAllProducts = value;
            }
        }

        protected virtual UserControl plArchiveProd
        {
            get
            {
                return this._plArchiveProd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plArchiveProd = value;
            }
        }

        protected virtual UserControl plCreateCat
        {
            get
            {
                return this._plCreateCat;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plCreateCat = value;
            }
        }

        protected virtual UserControl plImportModel
        {
            get
            {
                return this._plImportModel;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plImportModel = value;
            }
        }

        protected virtual UserControl plImportNew
        {
            get
            {
                return this._plImportNew;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plImportNew = value;
            }
        }

        protected virtual UserControl plImportShipping
        {
            get
            {
                return this._plImportShipping;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plImportShipping = value;
            }
        }

        protected virtual UserControl plImportUpdate
        {
            get
            {
                return this._plImportUpdate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plImportUpdate = value;
            }
        }

        protected virtual UserControl plProductDocs
        {
            get
            {
                return this._plProductDocs;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plProductDocs = value;
            }
        }

        protected virtual UserControl plProductImages
        {
            get
            {
                return this._plProductImages;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plProductImages = value;
            }
        }

        protected virtual UserControl plPurgeStore
        {
            get
            {
                return this._plPurgeStore;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plPurgeStore = value;
            }
        }

        protected virtual UserControl plShipping
        {
            get
            {
                return this._plShipping;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plShipping = value;
            }
        }

        protected virtual UserControl plXMLFile
        {
            get
            {
                return this._plXMLFile;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plXMLFile = value;
            }
        }

        protected virtual UserControl plZipDocs
        {
            get
            {
                return this._plZipDocs;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plZipDocs = value;
            }
        }

        protected virtual UserControl plZipFile
        {
            get
            {
                return this._plZipFile;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plZipFile = value;
            }
        }

        protected virtual Panel pnlBackup
        {
            get
            {
                return this._pnlBackup;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlBackup = value;
            }
        }

        protected virtual Panel pnlRestore
        {
            get
            {
                return this._pnlRestore;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlRestore = value;
            }
        }

        protected virtual RadioButton rbAllOrders
        {
            get
            {
                return this._rbAllOrders;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbAllOrders = value;
            }
        }

        protected virtual RadioButton rbAllProducts
        {
            get
            {
                return this._rbAllProducts;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbAllProducts = value;
            }
        }

        protected virtual RadioButton rbImportNew
        {
            get
            {
                return this._rbImportNew;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbImportNew = value;
            }
        }

        protected virtual RadioButton rbImportUpdate
        {
            get
            {
                return this._rbImportUpdate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbImportUpdate = value;
            }
        }

        protected virtual RadioButton rbProductDocs
        {
            get
            {
                return this._rbProductDocs;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbProductDocs = value;
            }
        }

        protected virtual RadioButton rbProductImages
        {
            get
            {
                return this._rbProductImages;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbProductImages = value;
            }
        }

        protected virtual RadioButton rbPurgeStore
        {
            get
            {
                return this._rbPurgeStore;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbPurgeStore = value;
            }
        }

        protected virtual RadioButton rbShipping
        {
            get
            {
                return this._rbShipping;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._rbShipping = value;
            }
        }
    }
}

